<?php

class ForgotPasswordForm extends Form
{
	public $MailModule = 'System/Data/Auth';
	public $MailTemplate = 'templates/setpassword.tpl';
	public $MailSubject = 'Change your password';
	
	public function ForgotPasswordForm()
	{
		parent::__construct();
		
		$this->Name = 'forgotpassword';
		$this->Title = translate('Forgot Password');
		$this->ButtonText = translate('Send Email');
		$this->MailSubject = translate('Change your password');
		
		// add controls to section
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'user', 'Title' => translate('Loginname'), 'IsBound' => false ) );
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'email', 'Title' => translate('Email'), 'IsBound' => false ) );
	}
	
	public function FormAction() 
	{
		if(session('user')!='')
		{
			$this->Messages[] = translate('Logged in as') . session('user') . '.';
			$this->HideControls = true;
		}
		else
		{
			// form load success?
			if( $this->LoadPostData())
			{
				$this->SendSetPasswordEmail( );
			}
		}
	}

	public function SendSetPasswordEmail()
	{
		$LoginName = $_POST['user'];
		$Email = $_POST['email'];
		
		if(UserManager::ConfirmEmail($LoginName, $Email))
		{
			$bean = UserManager::GetUser($LoginName);
			
			// send email with loginname and password
			$ConfirmURL = ConfirmRequestManager::GenerateRequestURL('http://sociorating.com/confirmsetpassword', $LoginName );
			$to = $bean->fullname . '<' . $_POST['email'] . '>';					
			$mailTemplate = a2o('TemplateLoader', array( 'Module' => $this->MailModule, 'Template' => $this->MailTemplate,
				'User' => $LoginName, 'FullName' => $bean->fullname, 'ConfirmURL' => $ConfirmURL ) );
			Mail::Send($to, $this->MailSubject, $mailTemplate->GetText());
			//mail($to, $this->MailSubject, $mailTemplate->translate( ));
			//$this->Messages[] = $mailTemplate->translate();
		
			// success
			$this->HideControls = true;
			$this->Messages[] = translate('An email has been sent to your account to change your password.');
		}
		else
			$this->Messages[] = translate('The username and email do not match.');
	}
}
